package com.zx.notification.admin.service.impl;

import com.zx.core.page.PageInfo;
import com.zx.core.wrapper.ObjectWrapper;
import com.zx.core.wrapper.PageWrapper;
import com.zx.notification.admin.service.INotificationPushTasksService;
import com.zx.notification.common.dao.NotificationPushTasksMapper;
import com.zx.notification.common.dao.SystemNotificationsMapper;
import com.zx.notification.common.enums.*;
import com.zx.notification.common.mapstruct.NotificationPushTasksMapStructMapper;
import com.zx.notification.common.model.NotificationPushTasks;
import com.zx.notification.common.model.SystemNotifications;
import com.zx.notification.common.param.NotificationPushTasksParam;
import com.zx.notification.common.vo.NotificationPushTasksVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * description: 通知推送任务管理服务实现类 <br>
 * create: 2025-10-17
 * </p>
 *
 * @author zhou  xun
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class NotificationPushTasksServiceImpl implements INotificationPushTasksService {

    private final NotificationPushTasksMapper notificationPushTasksMapper;
    private final SystemNotificationsMapper systemNotificationsMapper;
    private final NotificationPushTasksMapStructMapper notificationPushTasksMapStructMapper;

    @Override
    public PageWrapper<NotificationPushTasksVO> getNotificationPushTasksVOListByPage(NotificationPushTasksParam notificationPushTasksParam) {
        PageWrapper<NotificationPushTasksVO> result = new PageWrapper<>();
        try {
            PageInfo pageInfo = new PageInfo(notificationPushTasksParam.getPageNum(), notificationPushTasksParam.getPageSize());
            notificationPushTasksParam.setPageNum(pageInfo.getPageNum());
            notificationPushTasksParam.setPageSize(pageInfo.getPageSize());

            // 查询总数
            Long count = notificationPushTasksMapper.getNotificationPushTasksListCount(notificationPushTasksParam.getNotificationId(),
                    notificationPushTasksParam.getChannelType(),
                    notificationPushTasksParam.getMqType(),
                    notificationPushTasksParam.getStatus(),
                    notificationPushTasksParam.getCreateTimeStart(),
                    notificationPushTasksParam.getCreateTimeEnd());
            if (count > 0) {
                // 分页查询列表
                List<NotificationPushTasksVO> list = notificationPushTasksMapper.getNotificationPushTasksListByPage(notificationPushTasksParam.getNotificationId(),
                        notificationPushTasksParam.getChannelType(),
                        notificationPushTasksParam.getMqType(),
                        notificationPushTasksParam.getStatus(),
                        notificationPushTasksParam.getCreateTimeStart(),
                        notificationPushTasksParam.getCreateTimeEnd(),
                        notificationPushTasksParam.getPageNum(),
                        notificationPushTasksParam.getPageSize());

                // 设置描述性字段
                list.forEach(vo -> {
                    // 设置MQ类型描述
                    if (vo.getMqType() != null) {
                        MqTypeEnum mqTypeEnum = MqTypeEnum.getEnumByCode(vo.getMqType());
                        if (mqTypeEnum != null) {
                            vo.setMqTypeDesc(mqTypeEnum.getDesc());
                        }
                    }

                    // 设置渠道类型描述
                    if (vo.getChannelType() != null) {
                        ChannelTypeEnum channelTypeEnum = ChannelTypeEnum.getEnumByCode(vo.getChannelType());
                        if (channelTypeEnum != null) {
                            vo.setChannelTypeDesc(channelTypeEnum.getDesc());
                        }
                    }

                    // 设置消费模式描述
                    if (vo.getConsumeMode() != null) {
                        ConsumeModeEnum consumeModeEnum = ConsumeModeEnum.getEnumByCode(vo.getConsumeMode());
                        if (consumeModeEnum != null) {
                            vo.setConsumeModeDesc(consumeModeEnum.getDesc());
                        }
                    }

                    // 设置状态描述
                    if (vo.getStatus() != null) {
                        PushStatusEnum statusEnum = PushStatusEnum.getEnumByCode(vo.getStatus());
                        if (statusEnum != null) {
                            vo.setStatusDesc(statusEnum.getDesc());
                        }
                    }

                    // 设置推送策略描述
                    if (vo.getPushStrategy() != null) {
                        PushStrategyEnum pushStrategyEnum = PushStrategyEnum.getEnumByCode(vo.getPushStrategy());
                        if (pushStrategyEnum != null) {
                            vo.setPushStrategyDesc(pushStrategyEnum.getDesc());
                        }
                    }
                });

                // 设置返回结果
                result.setData(list);
            }
            result.setTotal(count);
            result.setCurrentPageNum(notificationPushTasksParam.getPageNum());
            result.setTotalPage(pageInfo);
        } catch (Exception e) {
            log.error("获取通知推送任务列表时发生错误", e);
            result.fail("获取通知推送任务列表失败，请稍后重试");
        }

        return result;
    }

    @Override
    public ObjectWrapper<NotificationPushTasksVO> selectNotificationPushTaskById(String notificationPushTaskId) {
        ObjectWrapper<NotificationPushTasksVO> result = new ObjectWrapper<>();
        try {
            // 验证通知推送任务是否存在
            NotificationPushTasks selectByPrimaryKey = notificationPushTasksMapper.selectByPrimaryKey(notificationPushTaskId);
            if (null == selectByPrimaryKey) {
                result.fail("通知推送任务不存在");
                return result;
            }

            NotificationPushTasksVO notificationPushTasksVO = notificationPushTasksMapStructMapper.notificationPushTasksToNotificationPushTasksVO(selectByPrimaryKey);

            // 设置描述性字段
            // 设置MQ类型描述
            if (notificationPushTasksVO.getMqType() != null) {
                MqTypeEnum mqTypeEnum = MqTypeEnum.getEnumByCode(notificationPushTasksVO.getMqType());
                if (mqTypeEnum != null) {
                    notificationPushTasksVO.setMqTypeDesc(mqTypeEnum.getDesc());
                }
            }

            // 设置渠道类型描述
            if (notificationPushTasksVO.getChannelType() != null) {
                ChannelTypeEnum channelTypeEnum = ChannelTypeEnum.getEnumByCode(notificationPushTasksVO.getChannelType());
                if (channelTypeEnum != null) {
                    notificationPushTasksVO.setChannelTypeDesc(channelTypeEnum.getDesc());
                }
            }

            // 设置消费模式描述
            if (notificationPushTasksVO.getConsumeMode() != null) {
                ConsumeModeEnum consumeModeEnum = ConsumeModeEnum.getEnumByCode(notificationPushTasksVO.getConsumeMode());
                if (consumeModeEnum != null) {
                    notificationPushTasksVO.setConsumeModeDesc(consumeModeEnum.getDesc());
                }
            }

            // 设置状态描述
            if (notificationPushTasksVO.getStatus() != null) {
                PushStatusEnum statusEnum = PushStatusEnum.getEnumByCode(notificationPushTasksVO.getStatus());
                if (statusEnum != null) {
                    notificationPushTasksVO.setStatusDesc(statusEnum.getDesc());
                }
            }

            // 设置推送策略描述
            if (notificationPushTasksVO.getPushStrategy() != null) {
                PushStrategyEnum pushStrategyEnum = PushStrategyEnum.getEnumByCode(notificationPushTasksVO.getPushStrategy());
                if (pushStrategyEnum != null) {
                    notificationPushTasksVO.setPushStrategyDesc(pushStrategyEnum.getDesc());
                }
            }

            // 查询关联的通知信息
            SystemNotifications notification = systemNotificationsMapper.selectByPrimaryKey(selectByPrimaryKey.getNotificationId());
            if (notification != null) {
                // 手动设置通知标题和内容
                notificationPushTasksVO.setNotificationTitle(notification.getTitle());
                notificationPushTasksVO.setNotificationContent(notification.getContent());
            }
            result.setData(notificationPushTasksVO);
        } catch (Exception e) {
            log.error("获取通知推送任务信息时发生错误", e);
            result.fail("获取通知推送任务信息失败，请稍后重试");
        }

        return result;
    }
}